package com.act.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * 分批执行
 */
public class SplitUtil {

    /**
     * 批次执行
     * @param ontCounts
     * @param list
     * @param function
     * @param <T>
     * @param <R>
     */
    public static <T,R> List<R>  runSplit(Integer ontCounts, List<T> list , Function<List<T>,List<R>> function){
        List<R> retList = new ArrayList<>();

        Integer forCounts = list.size() / ontCounts +(list.size() % ontCounts > 0 ?1:0);
        for(int i = 0 ; i < forCounts ; i++){
            List<T>  tempList = new ArrayList<>();
            if(i < forCounts -1){
                tempList.addAll(list.subList(i * ontCounts ,i*ontCounts + ontCounts));
            }else {
                tempList.addAll(list.subList(i * ontCounts ,list.size()));
            }
            List<R>  tempRetList =   function.apply(tempList);
            if(tempRetList != null && tempRetList.size() != 0){
                retList.addAll(tempRetList);
            }
        }
        return retList;
    }

}
